{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python语法基础  \n",
    "\n",
    "变量：字母、中文、类型查看、赋值、存储地址查询  \n",
    "\n",
    "输入：input()  \n",
    "\n",
    "输出：print()  \n",
    "\n",
    "注释符: #、三单引号'''或者三双引号\"\"\"引起来  \n",
    "      \n",
    "转义符: \\（续行） 、\\n （换行）\n",
    "\n",
    "输出格式： %、.format()、 f  \n",
    "\n",
    "string、list、tuple、dictionary、set\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、变量  \n",
    "\n",
    "在Python中，变量标记或指向一个值。当遇到变量时，Python将其替换为指向值。  \n",
    "\n",
    "#### 1、变量命名规则： \n",
    " \n",
    ">1.变量名的长度不受限制，但其中的字符必须是字母、数字、或者下划线（_），而不能使用空格、连字符、标点符号、引号或其他字符。  \n",
    " \n",
    ">2.变量名的第一个字符不能是数字，而必须是字母或下划线。  \n",
    "\n",
    ">3.Python区分大小写。  \n",
    "\n",
    ">4.不能将Python关键字用作变量名。  \n",
    "\n",
    "一般地，函数命名用小写字母，类命名用驼峰命名法。  \n",
    "\n",
    "#### 2、赋值语句并不会复制指向的值，而只是标记和重新标记既有值。因此，无论变量指向的对象有多大，多复杂，赋值语句的效率都非常高。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y,z=1,'China',\"中国\"  #可以同时给多个变量赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'y' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-1-5ed3d7553a17>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m()\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      2\u001B[0m \u001B[0;31m#查看赋值的变量的值\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 3\u001B[0;31m \u001B[0my\u001B[0m    \u001B[0;31m#或者使用print(y)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      4\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mNameError\u001B[0m: name 'y' is not defined"
     ]
    }
   ],
   "source": [
    "#查看赋值的变量的值\n",
    "y    #或者使用print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'中国'"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多重赋值的一个很实用的用途是交换两个变量的值\n",
    "y,z = z,y\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'North University of China'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "中文是可以直接做变量名的;\n",
    "作为变量的值除了数字和已经定义过的变量，一般都要加引号，否则报错；\n",
    "\n",
    "#像这样的多行被注释也可以是使用三双引号，单引号和双引号没有本质的区别，但是同时使用单引号和双引号时要区别。\n",
    "'''\n",
    "中北 = 'North University of China'  \n",
    "中北"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "str"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(中北)     #查看变量的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "140235583465872"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(中北)      #查看变量的存储地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'North University of China'"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "NUC = 中北   #把变量中北复制给NUC\n",
    "NUC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "140235583465872"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(NUC)     #查看变量NUC和中北的存储地址异同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、输入、格式化输出  \n",
    "\n",
    "input()  \n",
    "\n",
    "print(中北)与print('中北')的区别。  \n",
    "\n",
    "%格式化  \n",
    "\n",
    ".format()格式化  \n",
    "\n",
    "f格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "mobile=input('请输入你的手机号码：')  #接收来自键盘的输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "str"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(mobile)   #input()函数接收到的输入均为字符型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "mob = int(input('请输入你的手机号码：'))   #将接收到的手机号码转化为数值型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "int"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(mob)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中北大学的英文是 North University of China\n"
     ]
    }
   ],
   "source": [
    "print('中北大学的英文是 %s'%中北)  #%格式化，这里的中北是变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中北大学的英文是North University of China\n"
     ]
    }
   ],
   "source": [
    "print(f'中北大学的英文是{中北}')   #f格式化，也可以是F。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中北大学的英文是North University of China\n"
     ]
    }
   ],
   "source": [
    "print('中北大学的英文是{0}'.format(中北))   #format格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中北大学的英文是North University of China\n"
     ]
    }
   ],
   "source": [
    "nuc='中北大学'\n",
    "print('{1}的英文是{0}'.format(中北,nuc))   #多个变量的输出用索引更方便自由"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中北大学的英文是North University of China\n"
     ]
    }
   ],
   "source": [
    "print('{name}的英文是{ename}'.format(name='中北大学',ename='North University of China'))   #可以临时使用关键字格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 常见的转义符\n",
    "            转义字符\t描述\n",
    "            \\(在行尾时)\t续行符\n",
    "            \\\\\t反斜杠符号\n",
    "            \\'\t单引号\n",
    "            \\\"\t双引号\n",
    "            \\a\t响铃\n",
    "            \\b\t退格(Backspace)\n",
    "            \\e\t转义\n",
    "            \\000\t空\n",
    "            \\n\t换行\n",
    "            \\v\t纵向制表符\n",
    "            \\t\t横向制表符\n",
    "            \\r\t回车\n",
    "            \\f\t换页\n",
    "            \\oyy\t八进制数yy代表的字符，例如：\\o12代表换行\n",
    "            \\xyy\t十进制数yy代表的字符，例如：\\x0a代表换行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "yu = {'uppercase':'A','lowercase':'a'}\n",
    "print('The lowercase letter of {uppercase} is \\\n",
    "    {lowercase}'.format(**yu))  #字典格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.     陈某某      \n",
      "2.           陈某某\n",
      "3.陈某某           \n",
      "4.陈某某***********\n",
      "5.&&&&&&&&&&&陈某某\n"
     ]
    }
   ],
   "source": [
    "#填充和对齐: ^<>分别表示居中、左对齐、右对齐，后面带宽度 \n",
    "print('1.{:^14}'.format('陈某某'))  #共占位 14 个宽度，陈某某居中 \n",
    "print('2.{:>14}'.format('陈某某'))  #陈某某居右对齐 \n",
    "print('3.{:<14}'.format('陈某某'))  #陈某某居左对齐 \n",
    "print('4.{:*<14}'.format('陈某某')) #陈某某居左对齐其它的*填充 \n",
    "print('5.{:&>14}'.format('陈某某')) #陈某某居右对齐其它的&填充 \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 三、运算符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    运算符\t意义\n",
    "    x = y\t将y赋值给x\n",
    "    x + y\t返回x + y的值\n",
    "    x - y\t返回x - y的值\n",
    "    x * y\t返回x * y的值\n",
    "    x / y\t返回x / y的值\n",
    "    x // y\t返回x除y的整数部分\n",
    "    x % y\t返回x余y\n",
    "    abs(x)\t返回x的绝对值\n",
    "    int(x)\t返回x的整数值\n",
    "    float(x)\t返回x的浮点数\n",
    "    complex(re, im)\t定义复数\n",
    "    c.conjugate()\t返回复数的共轭复数\n",
    "    divmod(x, y)\t相当于(x//y, x%y)\n",
    "    pow(x, y)\t返回x的y次方\n",
    "    x ** y\t相当于pow()\n",
    "\n",
    "    x < y\t判断x是否小于y，是返回真否则返回假\n",
    "    x <= y\t判断x是否小于等于y，是返回真否则返回假\n",
    "    x > y\t判断x是否大于y，是返回真否则返回假\n",
    "    x >= y\t判断x是否大于等于y，是返回真否则返回假\n",
    "    x == y\t判断x是否等于y，是返回真否则返回假\n",
    "    x != y\t判断x是否不等于y，是返回真否则返回假\n",
    "    x is y\t判断x的地址（id）是否等于y，是返回真否则返回假\n",
    "    x is not y\t判断x的地址（id）是否不等于y，是返回真否则返回假\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、字符串（string）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s = '我是中国人！'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'中'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[2]      #访问字符串中的某个字符。Python索引从0开始，访问指定的n位置用:[n],适用于列表、元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'中国人！'"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[2:]     #切片均为左闭右开，[start:stop:step]，即取不到stop的值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s[1:5:2]   #从索引1到5中每隔1个取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sfz='340824197608088088'    \n",
    "s_1 = sfz[0:6]               #从身份证上提取出生地\n",
    "s_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_2 = sfz[6:14]           #提取出生年月\n",
    "s_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1 + s_2               #字符串的加法即是将两个字符串连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s_1*3                  #字符串的乘法即是对字符串进行多次复制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五、列表（List）  \n",
    "\n",
    "序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置，或索引，第一个索引是0，第二个索引是1，依此类推。  \n",
    "\n",
    "列表的元素不需要类型相同。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1 = [7,'中国','China',[3,'yubg','余本国']]     #list的元素类型不拘一格\n",
    "list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('list1[2]:',list1[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('list1[3]:',list1[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1[3][2]                  #二维索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### list填加元素：append()、extend()、insert()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1.append('NUC')     #直接在原列表上添加单个元素，不能写成list2=list1.append('NUC') 。指定的位置上添加呢？——insert(n,str)\n",
    "list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1.extend([2,'中北大学','NUC'])   #将两个列表连起来组成一个新的列表，即两个列表相加，效果同：list1 + [2,'中北大学','NUC']\n",
    "list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list0 = ['海南','China','中国']\n",
    "list1.insert(2,list0)    #注意第一个参数表示插入的位置，第二个参数是要插入的元素\n",
    "list1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 注意： 复制列表中的坑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_0 = [2,4,'HN']\n",
    "lis_1 = ['SX',7]\n",
    "\n",
    "lis_2 = lis_0         #复制一份\n",
    "lis_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_2.append(0)      #给lis_2添加一个元素0，观察变量lis_0的变化\n",
    "lis_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(id(lis_0),id(lis_2))       #观察lis_0和lis_2的存储地址是否相同\n",
    "#print('lis_0的ID:',id(lis_0),';','lis_2的ID:',id(lis_2))   #美化一下显示效果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 删除列表元素    list.pop(n)、list.remove(xx) 、list.remove()、del  list[n] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_0.pop()       #删除指定的索引位置元素，默认删除最后一个元素，并将删除的元素显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_3 = lis_0[:]       #重新复制：将lis_0中的每一个元素赋值给lis_3，再观察lis_3和lis_0的存储地址变化\n",
    "lis_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('lis_0和lis_3的存储地址分别为：',id(lis_0),id(lis_3))      #拷贝时宜使用这种方法，或者使用.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lis_4 = lis_0.copy()\n",
    "print('lis_0和lis_4的存储地址分别为：',id(lis_0),id(lis_4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(list1)\n",
    "print(len(list1))                 #查看删除后的list1的元素个数\n",
    "\n",
    "list1.remove('NUC')         #删除第一个‘NUC’，删除所有相同的元素？（for遍历remove）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(list1)\n",
    "len(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del list1[2:4]         #删除一个索引片段，也可以彻底删除整个列表\n",
    "list1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 修改list元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list1[0] = 9            #直接赋值修改\n",
    "list1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "   #####  Python list 常用方法总结 \n",
    "\n",
    "   ###### 一）添加新的元素\n",
    "    list.append(s)          在list 末尾增加一个元素s\n",
    "    list.insert（n,s）    在指定位置n添加元素s，如果指定的下标不存在则在末尾添加\n",
    "    list1.extend(list2)    合并list1、list2，把list2中元素追加在list1元素后面\n",
    "    \n",
    "   ###### 二）查看列表中的值\n",
    "    遍历列表\n",
    "           for i in list:\n",
    "               print(i)\n",
    "    list[n]        使用索引来访问列表中的值，同样也可以使用方括号的形式截取字符\n",
    "    list.count(x)  查看某个元素在这个列表里的个数，如果元素不存在，那么返回0\n",
    "    list.index(x)  找到这个元素的索引，返回第一个，若不存在会报错\n",
    "    \n",
    "   ###### 三）删除list 中的元素\n",
    "    list.pop(n)      删除指定索引的元素，默认删除最后一个元素，若不存在会报错\n",
    "    list.remove(x)   删除list 里面的一个元素，有多个相同的元素，删除第一个 \n",
    "    del list[n]      删除指定索引的元素或切片del list[n:m] \n",
    "    del list         删除整个列表， list删除后无法访问\n",
    "  \n",
    "    删除其中指定的某个元素的所有元素，如list0 = [1,2,1,3,1,2,4]，删除所有的1.\n",
    ">while i in list0:  \n",
    ">....list0.remove(i)  \n",
    ">print(list0)\n",
    "\n",
    "   ###### 四）排序和反转\n",
    "    list.reverse()          将列表反转\n",
    "    list.sort()             排序，默认升序\n",
    "    list.sort(reverse=True) 降序排列\n",
    "    注：list 中有字符串，数字时不能排序，排序针对同类型\n",
    "    \n",
    "   ###### 五）列表操作的函数\n",
    "    len(list)：列表元素个数 \n",
    "    max(list)：返回列表元素最大值 \n",
    "    min(list)：返回列表元素最小值 \n",
    "    list(seq)：将元组转换为列表\n",
    "    enumerate 用法（打印元素对应的下标）\n",
    ">lis = ['a','b','c']  \n",
    ">for i,v in enumerate(lis):  \n",
    ">....print('index=%s; value=%s'%(i,v))\n",
    "\n",
    "   ###### 六）list切片\n",
    "    list[n:m]     切片是不包含后面那个元素的值（顾头不顾尾,左开右闭）\n",
    "    list[:m]      如果切片前面一个值缺省的话，表示从开头开始取\n",
    "    list[n:]      如果切片后面的值缺省的话，表示取到末尾\n",
    "    list[:]       如果全部缺省，表示取全部元素\n",
    "    list[n:m:s]   s：步长,表示隔多少个元素取一次。步长是正数，从左往右取；步长是负数，从右往左取\n",
    "    注：切片同样适用于字符串。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 六、元组（tuple）  \n",
    "\n",
    "    Python 的元组与列表类似，不同之处在于元组的元素不能修改。\n",
    "    元组使用小括号，列表使用方括号。\n",
    "    元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。\n",
    "    \n",
    "    元组与字符串类似，下标索引从0开始，可以进行截取，组合等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tup1 = ('Google', 'Runoob', 97, [1,3])   #元素类可以多样化\n",
    "tup2 = (1,)                              #只有一元素的元组需要在元素后添加逗号\n",
    "tup3 = \"a\", \"b\", \"c\", \"d\";               #  不需要括号也可以\n",
    "print(tup1,tup2,tup3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tup1[3]             #访问元组中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple(tup1[3])        #将列表转化为元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(tup1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "max(tup3)             #取tuple中最大的值。取最小的：min(tuple)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tup2*3              #数乘表示复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tup1 + tup2          #元组的加法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 七、字典（dict）  \n",
    "\n",
    "    字典，又叫键值对，是一种通过关键字引用的数据结构，其键可以是数字、字符串、元组，这种结构类型也称之为映射。\n",
    "    每个键与值用冒号隔开（:），每对用逗号，每对用逗号分割，整体放在花括号中（{}）。\n",
    "    键必须独一无二，但值则不必。\n",
    "    值可以取任何数据类型，但必须是不可变的，如字符串，数或元组。\n",
    "    \n",
    "    字典基本的操作包括如下： \n",
    "    (1)len()：返回字典中键—值对的数量； \n",
    "    (2)d[k]:返回关键字对应的值，k表示键； \n",
    "    (3)d[k]=v:将值关联到键k上； \n",
    "    (4)del d[k]:删除键为k的项； \n",
    "    (5)key in d:键key是否在字典d中，是返回True，否则返回False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d={1:10,2:20,\"a\":12,5:\"hello\"}    #定义一个字典\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d1=dict(a=1,b=2,c=3)             #也可以如此定义\n",
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2=dict([['a',12],[5,'a4'],['hel','rt']]) #可以将二元列表/元组作为元素的列表/元组转化为字典d3=dict((['a',12],(5,'a4'),['hel','rt']))\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#字典是无序的。所以字典查的方式跟元组、列表有差异,不能使用索引\n",
    "d1['b']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d1.get('b')     # 也可用取键b的对应值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.items()       #items方法获取字典的项列表，是一个二元元组构成的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2.keys()       #获取字典的所有key的列表。d2.values()表示获取字典的所有value的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d1['c'] = 'yubg'    #【修改】字典hel键对应的值\n",
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2['y'] = 'yubg'   #【增/追加】新的键值对\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#【合并】两个字典用update()\n",
    "d1.update(d2)        #将字典d2中的键值对追加进d1\n",
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2.pop('y')      #【删除】key='y'的项\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "del d2['a']      # 删除键值对'a'\n",
    "d1.clear()       # 清空字典，让字典变成一个空字典\n",
    "del d            # 删除字典\n",
    "print(d2)\n",
    "len(d1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'd' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-7-062fea03e040>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m()\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 2\u001B[0;31m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0md\u001B[0m\u001B[0;34m)\u001B[0m      \u001B[0;31m#由于d已经被删除，所以已经不存在这个变量了\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      3\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mNameError\u001B[0m: name 'd' is not defined"
     ]
    }
   ],
   "source": [
    "print(d)      #由于d已经被删除，所以已经不存在这个变量了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#坑点1：键是不可变的，所以可以是数字、字符串、元组，但不能是列表\n",
    "dict1 = {['Name']: 'Runoob', 'Age': 7}                  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#坑点2：键必须是唯一的，只显示最后一次赋值的                 \n",
    "dict2 = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鸟'} \n",
    "dict2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 八、集合（set）  \n",
    "\n",
    "    set是一个无序且不重复的元素集合。\n",
    "    集合成员可以做字典中的键。集合支持用in和not in操作符检查成员，由len()内建函数得到集合的基数(大小)， 用 for 循环迭代集合的成员。但是因为集合本身是无序的，不可以为集合创建索引或执行切片(slice)操作。\n",
    "    set和dict一样，只是没有value，相当于dict的key集合，由于dict的key是不重复的，且key是不可变对象因此set也有如下特性：\n",
    ">1、不重复  \n",
    ">2、元素为不可变对象。列表和字典是不能作为集合的元素的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {11,'a',(4,1)}  #创建集合。创建空集合只能用s=set()，因为s={}创建的是空字典\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "se = {11, 22, 33}\n",
    "be = {22, 55}\n",
    "temp1 = se.difference(be)        #找到se中存在，be中不存在的元素，返回新值\n",
    "print(temp1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,1,4,2]\n",
    "set(a)                 #set()的一个主要功能就是过滤重复值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除 discard()、remove()、pop()\n",
    "b = set(a)\n",
    "b.pop()  #删除最小的值 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.add(1)      #添加新元素\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.remove(3)        #删除指定的元素\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.discard(2)       #删除指定的元素\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#取交集\n",
    "se = {11, 22, 33}\n",
    "be = {22, 55}\n",
    "\n",
    "temp1 = se.intersection(be)          #取交集，赋给新变量\n",
    "print(temp1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#取并集\n",
    "se = {11, 22, 33}\n",
    "be = {22,44,55}\n",
    "\n",
    "temp2 = se.union(be)   #取并集，并赋给新变量\n",
    "print(temp2)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#判断交、子、父集\n",
    "se = {11, 22, 33}\n",
    "be = {22}\n",
    "\n",
    "print('1、判断是否交集',se.isdisjoint(be))        #False，判断是否不存在交集（有交集False，无交集True）\n",
    "print('2、判断是否子集',se.issubset(be))          #False，判断se是否是be的子集合\n",
    "print('3、判断是否父集',se.issuperset(be))        #True，判断se是否是be的父集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#取差集\n",
    "se = {11, 22, 33}\n",
    "be = {22}\n",
    "\n",
    "se-be"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 十、模块与包  \n",
    "\n",
    "    模块，在Python可理解为对应于一个文件。在创建了一个脚本文件后，定义了某些函数和变量。你在其他需要这些功能的文件中，导入这模块，就可重用这些函数和变量。  \n",
    "    \n",
    "    包通常总是一个目录，可以使用import导入包，或者from + import来导入包中的部分模块。包目录下为首的一个文件便是 __init__.py。然后是一些模块文件和子目录，假如子目录中也有 __init__.py 那么它就是这个包的子包了。\n",
    "    \n",
    "    首先来看个例子：\n",
    " \n",
    ">  》nums = [1.23e+18, 1, -1.23e+18]  \n",
    ">  》sum(nums)   #求和  \n",
    ">   0.0\n",
    "\n",
    " \n",
    "结果怎么会是0呢？为什么不是1？  \n",
    "\n",
    "分析：浮点数的一个普遍问题是它们不能精确的表示十进制数，即使是最简单的数学运算也会产生小的误差。\n",
    "\n",
    "要解决这个问题，可以引入浮点问题的程序来解决——math模块。\n",
    "\n",
    "像这样他人写好的解决某一问题的程序代码，我们可以理解为模块、包或者叫库，这些程序代码命名保存。在使用时需要我们调用它，调用时使用import命令，格式为：\n",
    " >import  math       \n",
    "#文件名不需要写后缀.py\n",
    "\n",
    "在python下，有很多“好事者”已经编写好了很多丰富的模块、包，我们只需要奉行“拿来主义”就好，但前提是得知道里面包含了哪些函数，这些函数怎么使用！\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = [1.23e+18, 1, -1.23e+18]\n",
    "sum(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "nums = [1.23e+18, 1, -1.23e+18]\n",
    "math.fsum(nums)                   #在math模块下有一个求和函数fsum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math as m      #导入模块math，并给它取个别名m替代math，省去了后面的代码中经常输入math\n",
    "\n",
    "nums = [1.23e+18, 1, -1.23e+18]\n",
    "m.fsum(nums)                       #直接简用m替代了math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### 注意： from math import *   这种方式一建议使用，当导入的包较多时，很有可能不同的包中有相同的函数名，导致程序混乱。\n",
    "\n",
    "最后来看一个import——The Zen of Python："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import this  #python之禅"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 十、range()函数 、arange()函数\n",
    "\n",
    "    函数说明：range(start, stop[, step]) -> range object。\n",
    "    \n",
    "             根据start与stop指定的范围以及step设定的步长，生成一个【整数】序列。\n",
    "    \n",
    "    参数含义：start:计数从start开始。默认是从0开始。例如range（5）等价于range（0， 5）;\n",
    "             end:技术到end结束，但不包括end.例如：range（0， 5） 是[0, 1, 2, 3, 4]没有5\n",
    "             step：每次跳跃的间距，默认为1，setp不能是float。\n",
    "   >例如：range（0， 5） 等价于 range(0, 5, 1)。\n",
    "    \n",
    "    函数返回的是一个range object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r_1 = range(1,5,2)     #代表从1到5，间隔2(不包含5),若步长为1也可以省略步长，如r_2\n",
    "r_2 = range(1,5)       #代表从1到5(不包含5),若从0开始，也可以省略start，如r_3\n",
    "r_3 = range(5)         #代表从0到5(不包含5)\n",
    "\n",
    "list(r_3)              #在py3.x中，range函数已是一个容器，不再返回列表，当需要列表时可以转化，也可以转化成tuple。生成列表也可以直接使用for"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple(r_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#为什么会出错？\n",
    "range(1,10,0.1)       #range中的setp 不能是float。若要生成小数序列，则使用arange函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### arange()函数\n",
    "\n",
    "函数说明：arange([start,] stop[, step,], dtype=None)根据start与stop指定的范围以及step设定的步长，生成一个 ndarray。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np            #arange函数需要导入numpy包才能使用\n",
    "ar_1 = np.arange(1,2,0.2)\n",
    "ar_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 十一、流程控制  \n",
    "\n",
    "    编程语言中的流程控制语句分为以下几类：\n",
    "       >顺序语句  \n",
    "       >分支语句  \n",
    "       >循环语句  \n",
    "  \n",
    "  分支：  \n",
    ">if 判断条件1:  \n",
    ">....代码块1  \n",
    ">elif 判断条件2:  \n",
    ">....代码块2  \n",
    ">else:  \n",
    ">....代码块3   \n",
    "\n",
    "  循环：    \n",
    "> 1. while循环。当判断条件值为True时执行代码块，否则退出循环。  \n",
    ">while循环语句的基本形式如下：  \n",
    ">>while 判断条件:  \n",
    ">>....代码块  \n",
    ">2. for循环。for循环通常用于遍历序列(如list、tuple、range、str)、集合(如 set)和映射对象(如dict)。  \n",
    ">for循环的基本格式：  \n",
    ">>for 临时变量 in 可迭代对象:  \n",
    ">>....代码块  \n",
    ">3. 循环控制语句。循环控制语句可以更改循环体中程序的执行过程，如中断循环、跳过本次循环。  \n",
    ">>break》终止整个循环  \n",
    ">>continue》跳过本次循环，执行下一次循环\n",
    ">>pass》pass语句是个空语句，只是为了保持程序结构的完整性，没有什么特殊含义。pass语句并不是只能用于循环语句中，也可以用于分支语句中。\n",
    "\n",
    " \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#判断输入的成绩是属于哪个档次：\n",
    "bz = ['优','良','中','及格','差']    #成绩层次分类\n",
    "s = int(input('请输入分数：'))       #接收键盘输入\n",
    "\n",
    "if s < 60:\n",
    "    print(bz[4])\n",
    "elif s >= 60 and s<70:\n",
    "    print(bz[3])\n",
    "elif s >= 70 and s<80:\n",
    "    print(bz[2])\n",
    "elif s >= 80 and s<90:\n",
    "    print(bz[1])\n",
    "else:\n",
    "    print(bz[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#for循环主要是遍历\n",
    "a = 'I\\'d like to stay in China'\n",
    "b = list(set(a))                  #将字符串a中的字符去重，再做成列表\n",
    "i=0\n",
    "for i in range(len(b)):\n",
    "    print(i,':',b[i])             #拍个序号打印\n",
    "    i+=1                          #i=i+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    #注意本例将进入死循环 \n",
    "    '''\n",
    "    i=1 \n",
    "    while i<10:\n",
    "        if i%2 == 0:      #由当i=2时进入了continue，并没有自身加1，所以一直在i=2循环\n",
    "            continue      #表示continue之后的代码不再执行，再从while开始下一轮的循环；若是break，不仅之后的代码不执行，整个循环都结束。\n",
    "        print(i, end=' ')   \n",
    "        i+=1\n",
    "    '''\n",
    "    #请先运行下面的代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输出小于10的奇数\n",
    "i=1 \n",
    "while i<10:\n",
    "    if i%2 == 0:\n",
    "        i+=1\n",
    "        continue\n",
    "    print(i, end=' ')   #将结果打印在一行\n",
    "    i+=1 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 十二、函数  \n",
    "\n",
    "    函数的定义：在 Python 中，函数的定义基本形式如下。\n",
    "\n",
    ">def function(params):  \n",
    ">....\"\"\"  \n",
    ">.... 这里的文字是函数文档注释，便于help()函数调用查阅。  \n",
    ">....\"\"\"  \n",
    "\n",
    ">....block  \n",
    ">....return expression/value "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add(a,b=0):         #定义一个加法的函数。有两个变量，其中第二个变量不赋值时默认是0\n",
    "    \"\"\"\n",
    "    说明：此函数主要是计算两个数的加法。当给的两个变量同时是数值或者元组、列表、字符串，但是字典和集合不可以，因为字典和集合没有“+”运算。\n",
    "    1、此函数有两个变量，当仅输入一个变量时，第二个变量默认为0；\n",
    "    2、当输入两个变量时，将是此两个变量的“+”运算。\n",
    "    \n",
    "    \"\"\"\n",
    "    c = a + b           \n",
    "    return c\n",
    "\n",
    "add(2,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Lambda    匿名函数，也叫行内函数   \n",
    "\n",
    "lambda 它只是一个表达式，函数体比 def 简单很多。lambda 表达式运行起来像一个函 数。lambda 函数的用途：   \n",
    ">1、对于单行函数，使用 lambda 可以省去定义函数的过程，让代码更加精简。   \n",
    "2>、在非多次调用函数的情况下，lambda 表达式即用即得，提高性能。   \n",
    "\n",
    "格式：  \n",
    ">lambda 参数:表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = lambda x : x+2      #定义了一个函数 f(x)=x+2 \n",
    "g = lambda x,y : x+y    #定义了一个函数 g(x,y)=x+y \n",
    "\n",
    "print(f(0))\n",
    "print(g(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#filter()过滤函数：指在给定的条件上（参数1）对某域（参数2）进行过滤\n",
    "fl = filter(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9])   #对列表中的个元素是3的倍数进行过滤,并返回符合条件的，但返回的是容器\n",
    "list(fl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "reduce()累计计算函数:\n",
    "   reduce(func(), Z)    把函数func()作用在序列Z上累计计算。其效果就是： \n",
    "        reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4) \n",
    "但需要调用functools包\n",
    "\"\"\"\n",
    "from functools import reduce\n",
    "a_1 = reduce(lambda a,b: a * b, [1, 2, 3, 4, 5, 6, 7, 8, 9])    #计算9！\n",
    "a_2 = reduce(lambda a,b: a + b, [1, 2, 3, 4, 5, 6, 7, 8, 9])    #计算1+2+...+9的和\n",
    "\n",
    "print(\"9!:\",a_1)\n",
    "print(\"求和：\",a_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "映射函数map()\n",
    "map(func，S)    将传入的函数 func 依次作用到序列 S 的每个元素，并把结果作为新的序列返回。 \n",
    "函数 func 在 S 域上遍历，在 Python 3.x 中 map()是一个容器，返回时需要用 list 调用才显示数据，显示的是 func 作用后的结果数据。 \n",
    "'''\n",
    "m = map(lambda x: x+1, [1,2,3])\n",
    "list(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str1=r'hello \\npython' \n",
    "str2='hello \\npython'\n",
    "str3=\"hello python\\n\"*3\n",
    "str4=u\"大家好\"\n",
    "\n",
    "print(\"str1: {0}\".format(str1))\n",
    "print(\"str2: {0}\".format(str2))\n",
    "print(\"str3: {0}\".format(str3))\n",
    "print(\"str4: {0}\".format(str4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=int(input())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-3f9ee60f",
   "language": "python",
   "display_name": "PyCharm (2020_C)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}